// pages/task/task.js
import { request } from "../../utils/index.js";
var app = getApp()  
Page({

  /**
   * 页面的初始数据
   */
  data: {

    option1: [
      { text: '综合排序', value: 0 },
      { text: '最新排序', value: 1 },
      { text: '距离排序', value: 2 },
    ],
    title:"类型",
    title1:"综合排序",
    value1: 0,
    activeKey: 0,
    categoryList:[], // 分类列表
    childrenList:[], // 二级分类列表
    taskList:[], // 任务列表
    page:1, // 分页
    latitude:'34.71982010079598', // 经度
    longitude:'113.63797000000001', // 纬度
    scene:'0', // 排序：0=综合排序，1=最新排序，2=距离排序
    category_ids:"4", // 二级分类id（英文逗号分隔）
    gender_require:0, // 性别要求：0=不限，1=男，2=女
    time_between:'0', // 上班时段：0=不限，1=上午，2=下午，3=晚上
    genderList: [
      { name: '不限', value: 0 ,checked:'false' },
      { name: '男', value: 1 ,checked:'false'},
      { name: '女', value: 2 ,checked:'false'}
    ],
    timeList: [
      { name: '不限', value: 0 ,checked:'false' },
      { name: '上午', value: 1 ,checked:'false'},
      { name: '下午', value: 2 ,checked:'false'},
      { name: '晚上', value: 3,checked:'false' }
    ],
  },

  // 定位
  getLocation:function() {
    // console.log(e)
    var that = this
    wx.getLocation({
      type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: function(res){
      console.log(res)     
      that.setData({
          longitude:res.longitude,
          latitude:res.latitude        
        })
      }
　  })
  },

  // 获取城市
  loadCity:function(longitude,latitude){  
    var page =this  
    wx.request({  
      url: 'https://api.map.baidu.com/geocoder/v2/?ak=您的ak &location='+latitude+','+longitude+'&output=json',  
      data: {},  
      header:{  
        'Content-Type':'application/json'  
      },  
      success: function(res){  
        // success  
        console.log(res);  
        var city=res.data.result.addressComponent.city;  
        page.setData({city:city});  
      },  
      fail: function() {  
        // fail  
      },  
      complete: function() {  
        // complete  
      }  
    })  
  },

  // 分类列表
  getCategoryTree () {
    request({ url: "/api/task/categoryTree" })
    .then(result => {
      let item = result.data[0];
      this.setData({
        categoryList:result.data,
        childrenList:item.children
      })
    })
  },

  // 任务列表
  getTask() {
    var that = this;
    request({ url: "/api/task/index",data:{
        page:that.data.page,
        lat:that.data.latitude,
        lng:that.data.longitude,
        category_ids:that.data.category_ids,
        scene:that.data.scene,
        gender_require:that.data.gender_require,
        time_between:that.data.time_between,
      }
    })
    .then(result => {
      that.setData({
        taskList:that.data.taskList.concat(result.data.list)
      })
    })
  },


  // 点击一级分类
  onChangeCategory(event){
    let item = this.data.categoryList[event.detail];
    this.setData({childrenList:item.children})
  },

  // 点击二级分类
  onTwoCategory(event) {
    this.selectComponent('#item').toggle();
    let category_ids = event.currentTarget.dataset.value.id;
    let title = event.currentTarget.dataset.value.name;
    this.setData({
      title:title,
      category_ids:category_ids
    });
    this.data.page = 1;
    this.data.taskList = [];
    this.getTask();
  },

  // 点击排序
  onChangeScene(event) {
    let item = this.data.option1[event.detail];
    this.setData({
      title1:item.text,
      value1:event.detail,
      scene:event.detail
    })
    this.data.page = 1;
    this.data.taskList = [];
    this.getTask();
  },

  // 选择性别要求
  onChange(e) {
    console.log(e)
    var that = this;
    let genderList = this.data.genderList;
    for (var i = 0; i < genderList.length; ++i) {
      if(genderList[i].value==e.detail[0]){
        genderList[i].checked=true;
      }else{
        genderList[i].checked = false;
      }
    }
    that.setData({genderList:genderList,gender_require:e.detail[0]})
  },

  // 选择上班时段
  onChange2(e) {
    console.log(e)
    var that = this;
    let timeList = this.data.timeList;
    for (var i = 0; i < timeList.length; ++i) {
      if(timeList[i].value==e.detail[0]){
        timeList[i].checked=true;
      }else{
        timeList[i].checked = false;
      }
    }
    that.setData({timeList:timeList,time_between:e.detail[0]})
  },

  onConfirm() {
    this.selectComponent('#item2').toggle();
    this.data.page = 1;
    this.data.taskList = [];
    this.getTask();
  },


  // 点击列表
  onItem(item){

    var token = wx.getStorageSync("token");
    if (!token) {
      return wx.navigateTo({
        url: '/pages/login/login',
      })
    }

    let id = item.currentTarget.dataset.value.id;
    wx.navigateTo({
      url: '/pages/taskDetail/taskDetail?id='+id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getLocation();
    this.getCategoryTree();
    this.getTask();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.page++;
    this.getTask();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})